#include "system.h"
#include "bsp.h"
#include "drv_led.h"
#include "drv_beep.h"
#include "drv_key.h"
#include "drv_rtc.h"
#include "drv_spi_w25qxx.h"
// #include "drv_qspi_w25qxx.h"
#include "drv_ina3221.h"
#include "drv_sht30.h"
#include "drv_sdcard.h"
#include "drv_lcd.h"
#include "drv_touch.h"
#include "drv_lan8720.h"

#include "usb_port.h"

#include "os_sal.h"
#include "loger.h"
#include "shell.h"
#include "mem_sal.h"
#include "dfs.h"
#include "lv_port.h"
#include "lvgl.h"

#define START_TASK_PRIO		4			//任务优先级
#define START_STK_SIZE 		256  		//任务堆栈大小	
os_task_handle_t StartTask_Handler;		//任务句柄
void start_task(void *pvParameters);	//任务函数

#define MAIN_TASK_PRIO		6			//任务优先级
#define MAIN_STK_SIZE 		512  		//任务堆栈大小	
os_task_handle_t main_task_handler;		//任务句柄
void main_task(void *pvParameters);		//任务函数

#define WATCH_TASK_PRIO		7			//任务优先级
#define WATCH_STK_SIZE 		512  		//任务堆栈大小	
os_task_handle_t watch_task_handler;	//任务句柄
void watch_task(void *pvParameters);	//任务函数

#define GUI_TASK_PRIO		5			//任务优先级
#define GUI_STK_SIZE 		1024  		//任务堆栈大小	
os_task_handle_t gui_task_handler;	    //任务句柄
void gui_task(void *pvParameters);	    //任务函数




int main(void)
{
    bsp_init();

    drv_led_init();
    drv_beep_init();
    drv_key_init();
    drv_rtc_init();
    
    /* 创建开始任务 */
    os_task_create((os_task_t )start_task,                  //任务函数
                    (void*            )NULL,                //传递给任务函数的参数
                    (const char*      )"start_task",        //任务名称
                    (uint16_t         )START_STK_SIZE,      //任务堆栈大小
                    (uint32_t         )START_TASK_PRIO,     //任务优先级
                    (os_task_handle_t )&StartTask_Handler);	//任务句柄              
    os_scheduler_start();          //开启任务调度
    
}


//开始任务任务函数
void start_task(void *pvParameters)
{
    /* 以下外设初始化是可能会有系统堆栈空间不足的问题,因此放到任务中进行初始化 */

    drv_spi_w25qxx_init();
    // drv_qspi_w25qxx_init();

    drv_lcd_init();
    drv_lcd_clear(LCD_COLOR_WHITE);
    // drv_lcd_fill_color(10, 10, 100, 100, LCD_COLOR_BLUE);
    drv_lcd_backlight_on();

    drv_touch_init();
    drv_ina3221_init();
    drv_sht30_init();

    drv_sdcard_init();

    os_critical_enter();           //进入临界区
    /* 创建main任务 */
    os_task_create((os_task_t)main_task,
                   (void *)NULL,
                   (const char *)"main_task",
                   (uint32_t)MAIN_STK_SIZE,
                   (uint32_t)MAIN_TASK_PRIO,
                   (os_task_handle_t)&main_task_handler);
    /* 创建watch任务 */
    os_task_create((os_task_t)watch_task,
                   (void *)NULL,
                   (const char *)"watch_task",
                   (uint32_t)WATCH_STK_SIZE,
                   (uint32_t)WATCH_TASK_PRIO,
                   (os_task_handle_t)&watch_task_handler);

    /* 创建gui任务 */
    os_task_create((os_task_t)gui_task,
                   (void *)NULL,
                   (const char *)"gui_task",
                   (uint32_t)GUI_STK_SIZE,
                   (uint32_t)GUI_TASK_PRIO,
                   (os_task_handle_t)&gui_task_handler);

    // os_task_delete(StartTask_Handler, NULL); //删除开始任务

    loger_init();
    shell_init();
    mem_init();
    // dfs_init();
    // dfs_user_init();
    

    os_critical_exit();            //退出临界区

    usb_port_hw_init();
    // usbd_hid_custom_init();
    // usbd_msc_init();
    usbd_cdc_vcp_init();
    

	while (1)
	{
		// key_loop();
		// os_delay(10);

        drv_led_led0_on();
        os_delay(500);

        drv_led_led0_off();
        os_delay(500);

	}
}

void main_task(void *pvParameters)
{
    int i;
    int16_t ina_vol, ina_ele;
    float sht_tmp, sht_hum;


    drv_beep_on();
    os_delay(10);
    drv_beep_off();

	while(1)
    {
        if (drv_key_pres_key0())
        {
            printf("key0 press\r\n");
        }
        if (drv_key_pres_key1())
        {
            printf("key1 press\r\n");
        }

        /* ina电压电流 */
        for (i = 2; i <= 3; i++)
        {
            drv_ina3221_get_value(i, &ina_vol, &ina_ele);
            // printf("INA[%d], vol=%d.%03dmV, ele=%dmA; ", i, ina_vol/1000, ina_vol%1000, ina_ele);
        }
        // printf("\r\n");
        
        /* sht32 温湿度 */
        drv_sht30_get_temp_hum(&sht_tmp, &sht_hum);
        // printf("temperature = %.2f'C, humidity = %.2f%%\r\n", sht_tmp, sht_hum);
        // LOG_A("MAIN", "temperature = %.2f'C, humidity = %.2f%%", sht_tmp, sht_hum);
        // LOG_E("MAIN", "temperature = %.2f'C, humidity = %.2f%%", sht_tmp, sht_hum);
        // LOG_W("MAIN", "temperature = %.2f'C, humidity = %.2f%%", sht_tmp, sht_hum);
        // LOG_I("MAIN", "temperature = %.2f'C, humidity = %.2f%%", sht_tmp, sht_hum);
        // LOG_D("MAIN", "temperature = %.2f'C, humidity = %.2f%%", sht_tmp, sht_hum);
        // LOG_RAW("MAIN", "temperature = %.2f'C, humidity = %.2f%%", sht_tmp, sht_hum);

        os_delay(1000);

    }
}

void watch_task(void *pvParameters)
{
    uint32_t cnt = 0;

    while (1)
    {

        cnt++;

        if (cnt == 10)
        {
            drv_led_led1_on();
        }
        if (cnt == 15)
        {
            drv_led_led1_off();
        }
        if (cnt >= 100)
        {
            cnt = 0;
        }

        os_delay(10);
    }    
}


#include "lv_examples.h"


void gui_task(void *pvParameters)
{
    lv_port_init();

    os_delay(10);

    // lv_demo_benchmark();

    lv_demo_widgets();

    // lv_demo_music();

    while (1)
    {
        lv_task_handler();
        os_delay_unit(10);
        // os_delay(5);
    }
}



